package com.dshore.messagecenter.controller.basedata;

import java.io.UnsupportedEncodingException;
import java.util.regex.Pattern;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.dshore.messagecenter.log.LogAnnotation;
import com.dshore.messagecenter.model.basedata.MdTemplate;
import com.dshore.messagecenter.model.system.ResponseResult;
import com.dshore.messagecenter.service.basedata.TemplateManagementService;
import com.dshore.messagecenter.util.Page;
/**
 * 模板管理
 * @author Administrator
 * @param <T>
 *
 */
@Controller
@RequestMapping(value="/template")
public class TemplateManagementController<T> {
	@Autowired
	private TemplateManagementService templateManagementService;
	/**
	 * 展示页面
	 * @return
	 */
	@RequestMapping(value="/toTemplate")
	public String module() {
		return "basedata/mdTemplate";
	}
	/**
	 * 带参页面
	 * @param param
	 * @param key session中的值
	 * @return
	 */
	@RequestMapping(value = "/toByService", method = RequestMethod.GET)
	public ModelAndView addService(String param,
			@RequestParam(value = "key",required = false) String key){
		ModelAndView mv = new ModelAndView("mdTemplate");
		mv.addObject("param", param);
		mv.addObject("key",key);
		return mv;
	}
	/**
	 * 新增模版
	 * @param template
	 */
	@LogAnnotation(methodType="新增",targetType="模板",argsName="templateName")
	@RequestMapping(value="/addTemplate",method = RequestMethod.POST)
	@ResponseBody
	public ResponseResult<Void> addTemplate(MdTemplate template){
		if(check(template)){
			try {
				return	templateManagementService.addTemplate(template);
			}catch(Exception e){
				return new ResponseResult<Void>(400,"新增失败");
			}
		}else {
			return new ResponseResult<Void>(400,"新增失败");
		}
	}
	/**
	 * 修改模版
	 * @param template
	 */
	@LogAnnotation(methodType="修改",targetType="模板",argsName="templateName")
	@RequestMapping(value="/changeTemplate",method=RequestMethod.POST)
	@ResponseBody
	public ResponseResult<Void> changeTemplate(MdTemplate template){
		if(check(template)){
			try {
				return	templateManagementService.changeTemplate(template);
			}catch(Exception e) {
				return new ResponseResult<Void>(400,"修改失败");
			}
		}else {
			return new ResponseResult<Void>(400,"修改失败");
		}
	}
	
	/**
	 * 删除模版
	 * @param id
	 */
	@LogAnnotation(methodType="删除",targetType="模板",argsName="ids",fieldName="TEMPLATE_NAME",tableName="T_MD_TEMPLATE")
	@RequestMapping(value="/deleteByIds",method=RequestMethod.POST)
	@ResponseBody
	public ResponseResult<Void> delTemplate(String ids) {
		 return templateManagementService.delTemplate(ids);	
	}
	
	/**
	 * 查询单个数据
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/getById",method=RequestMethod.POST)
	@ResponseBody
	public MdTemplate getById(String id) {
		return templateManagementService.getById(id);		
	}
	
	/**
	 * 展示所有模板数据
	 * @param template
	 * @return
	 */
	@RequestMapping(value="/getTemplate", method = RequestMethod.POST)
	@ResponseBody
	public Page<MdTemplate> getTemplate(@RequestBody MdTemplate template ){
		String str ="^[A-Za-z0-9\u4e00-\u9fa5]{1,32}+$";
		if(StringUtils.isEmpty(template.getTemplateName())||Pattern.compile(str).matcher(template.getTemplateName()).find()) {
			try {
				return templateManagementService.queryListAll(template);
			}catch(Exception e) {
				return null;
			}
		}else {
			return null;
		}
	}
	
	/**
	 * 查询模板名称是否重复
	 * @param templatename
	 * @return
	 */
	@RequestMapping(value="/checkName", method = RequestMethod.POST)
	@ResponseBody
	public String checkName(String id,String templatename) {
		String str ="^[A-Za-z0-9\u4e00-\u9fa5]{1,32}+$";
		if(Pattern.compile(str).matcher(templatename).find()) {
			return templateManagementService.checkName(id,templatename);
		}else {
			return "false";
		}
	}
	
	//校验
	private  boolean check(MdTemplate template) {
		String str ="^[A-Za-z0-9\u4e00-\u9fa5]{1,32}+$";
		if(StringUtils.isNotBlank(template.getTemplateName())
						&&StringUtils.isNotBlank(template.getTemplateContent())
						&&StringUtils.isNotBlank(template.getSampleData())
						
			) {
				template.setTemplateContent(decode(template.getTemplateContent()));
				template.setSampleData(decode(template.getSampleData()));
				if(template.getTemplateContent().length()<=2000&&template.getSampleData().length()<=2000) {
					if(Pattern.compile(str).matcher(template.getTemplateName()).find()&&isJson(template.getSampleData())){
						return true;
					}else {
						return false;
					}	
				}else {
					return false;
				}
		}else {
			return false;
		}		
	}
	/**
	 * 校验json字符
	 * @param str
	 * @return
	 */
	private boolean isJson(String str) {
		try {
			JSONObject.parseObject(str);
			return true;
		}catch(Exception e) {
			return false;
		}
	}
	
	public static String decode(String str) {
		try {
			return new String(Base64.decodeBase64(str.getBytes("UTF-8")),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			return null;
		}
	}
}
